def say_hello(n):
    if n == 0:
        return
    print("你好")
    # 在这里调用自己，让 n 减 1
    say_hello(n-1)
say_hello(3)

def sum_n(n):
    if n == 1:
        return 1
    return n + sum_n(n-1)  # 调用自己

print(sum_n(3))

def fact(n):
    if n == 0 or n == 1:
        return 1
    return n * fact(n-1) # 调用自己
print(fact(4))

add = lambda a, b: a+b
print(add(2, 3))

print((lambda x: x**2)(10))

nums = [1, 2, 3]
doubled = list(map(lambda x: x*2, nums))
print(doubled)  # [2, 4, 6]

nums = [1, 2, 3, 4, 5]
evens = list(filter(lambda x:x%2==0, nums))
print(evens)  # [2, 4]

get_max = lambda a, b: a if a>b else b  # 用三元表达式
print(get_max(5, 3))

nums = [3, 1, 2]
sorted_nums = sorted(nums, key=lambda x: x)
print(sorted_nums)  # [1, 2, 3]

d = {'a': 3, 'b': 1, 'c': 2}
result = sorted(d, key=lambda k: d[k])
print(result)  # ['b', 'c', 'a']

def make_func(x):
    def inner():
        print(x)  # 使用外面的 x
    return inner # 返回 inner

f = make_func(100)
f()  # 应该输出 100

def adder(a, b):
    def inner():
        return a+b # a + b
    return inner

f = adder(3, 5)
print(f())  # 8

def counter():
    count = [0]  # 用列表装数字
    def inner():
        count[0] = count[0] + 1
        return count[0]
    return inner

c = counter()
print(c())  # 1
print(c())  # 2










